package clues.bihu.cluesaccident

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import java.io.*

/**
 *@Date 2018/7/17
 *@Author Mr.WEI
 *@Description
 **/
object Preference {
    val TOKEN = "token"
    val NAME = "Name"
    val ACCOUNT = "account"
    val AGENTID = "AgentId"
    val TOPAGENTID = "topAgentId"
    val ROLEID = "roleId"
    val ROLETYPE = "roleType"
    val DEPAERMENTID = "departmentId"
    val SECRETKEY = "secretKey"
    val CITYID = "cityId"
    val IS_NOT_FIRST_START = "isNotFirstStart"
    val ROLENAME = "roleName"
    val LOGIN_JSON = "login_json"

    private val prefs: SharedPreferences by lazy {
        ClueBaseApp.appContext?.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)!!
    }

    @SuppressLint("CommitPrefEdits")
    fun saveInt(name: String, value: Int) = with(prefs.edit()) {
        putInt(name, value).apply()
    }

    @SuppressLint("CommitPrefEdits")
    fun catchInt(name: String, default: Int): Int = with(prefs) {
        getInt(name, default)
    }

    @SuppressLint("CommitPrefEdits")
    fun saveString(name: String, value: String) = with(prefs.edit()) {
        putString(name, value).apply()
    }

    @SuppressLint("CommitPrefEdits")
    fun catchString(name: String, default: String): String = with(prefs) {
        getString(name, default)
    }

    @SuppressLint("CommitPrefEdits")
    fun saveLong(name: String, value: Long) = with(prefs.edit()) {
        putLong(name, value).apply()
    }

    @SuppressLint("CommitPrefEdits")
    fun catchLong(name: String, default: Long): Long = with(prefs) {
        getLong(name, default)
    }

    @SuppressLint("CommitPrefEdits")
    fun saveFloat(name: String, value: Float) = with(prefs.edit()) {
        putFloat(name, value).apply()
    }

    @SuppressLint("CommitPrefEdits")
    fun catchFloat(name: String, default: Float): Float = with(prefs) {
        getFloat(name, default)
    }

    @SuppressLint("CommitPrefEdits")
    fun saveBoolean(name: String, value: Boolean) = with(prefs.edit()) {
        putBoolean(name, value).apply()
    }

    @SuppressLint("CommitPrefEdits")
    fun catchBoolean(name: String, default: Boolean): Boolean = with(prefs) {
        getBoolean(name, default)
    }

//    @SuppressLint("CommitPrefEdits")
//    fun saveJson(name: String, value: Any) = with(prefs.edit()) {
//        putString(name, GsonUtil.gson().toJson(value)).apply()
//    }

//    @SuppressLint("CommitPrefEdits")
//    fun <T> getJsonObject(name: String, default: String, mClass: Class<T>): T = with(prefs) {
//        JsonParseUitl.parseObject(getString(name, default), mClass)
//    }


    /**
     * 删除全部数据
     */
    fun clearPreference() {
        prefs.edit().clear().apply()
    }

    /**
     * 根据key删除存储数据
     */
    fun clearPreference(key: String) {
        prefs.edit().remove(key).apply()
    }

    /**
     * 序列化对象

     * @param person
     * *
     * @return
     * *
     * @throws IOException
     */
    @Throws(IOException::class)
    private fun <A> serialize(obj: A): String {
        val byteArrayOutputStream = ByteArrayOutputStream()
        val objectOutsaveStream = ObjectOutputStream(
                byteArrayOutputStream)
        objectOutsaveStream.writeObject(obj)
        var serStr = byteArrayOutputStream.toString("ISO-8859-1")
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8")
        objectOutsaveStream.close()
        byteArrayOutputStream.close()
        return serStr
    }

    /**
     * 反序列化对象

     * @param str
     * *
     * @return
     * *
     * @throws IOException
     * *
     * @throws ClassNotFoundException
     */
    @Suppress("UNCHECKED_CAST")
    @Throws(IOException::class, ClassNotFoundException::class)
    private fun <A> deSerialization(str: String): A {
        val redStr = java.net.URLDecoder.decode(str, "UTF-8")
        val byteArrayInsaveStream = ByteArrayInputStream(
                redStr.toByteArray(charset("ISO-8859-1")))
        val objectInsaveStream = ObjectInputStream(
                byteArrayInsaveStream)
        val obj = objectInsaveStream.readObject() as A
        objectInsaveStream.close()
        byteArrayInsaveStream.close()
        return obj
    }


    /**
     * 查询某个key是否已经存在
     *
     * @param key
     * @return
     */
    fun contains(key: String): Boolean {
        return prefs.contains(key)
    }

    /**
     * 返回所有的键值对
     *
     * @param context
     * @return
     */
    fun getAll(): Map<String, *> {
        return prefs.all
    }

    fun saveUserInfo(data: String) {
//        val parseObject = JsonParseUitl.parseObject(data, LoginBean::class.java)
//        Preference.saveString(Preference.TOKEN, parseObject.token)
//        Preference.saveString(Preference.NAME, parseObject.name)
//        Preference.saveString(Preference.ROLENAME, parseObject.roleName)
//        Preference.saveString(Preference.ACCOUNT, parseObject.account)
//        Preference.saveInt(Preference.AGENTID, parseObject.agentId)
//        Preference.saveInt(Preference.TOPAGENTID, parseObject.topAgentId)
//        Preference.saveInt(Preference.ROLEID, parseObject.roleId)
//        Preference.saveInt(Preference.ROLETYPE, parseObject.roleType)
//        Preference.saveInt(Preference.DEPAERMENTID, parseObject.departmentId)
//        Preference.saveString(Preference.SECRETKEY, parseObject.secretKey)
//        Preference.saveString(Preference.CITYID, parseObject.cityId)
//        Preference.saveString(Preference.LOGIN_JSON, data)
    }

    fun clearUserInfo() {
        Preference.saveString(Preference.TOKEN, "")
        Preference.saveString(Preference.NAME, "")
        Preference.saveString(Preference.ACCOUNT, "")
        Preference.saveInt(Preference.AGENTID, -1)
        Preference.saveInt(Preference.TOPAGENTID, -1)
        Preference.saveInt(Preference.ROLEID, -1)
        Preference.saveInt(Preference.ROLETYPE, -1)
        Preference.saveInt(Preference.DEPAERMENTID, -1)
        Preference.saveString(Preference.SECRETKEY, "")
        Preference.saveString(Preference.CITYID, "")
        Preference.saveString(Preference.ROLENAME, "")
        Preference.saveString(Preference.LOGIN_JSON, "")
    }

    fun canNotFollow(state: Int): Boolean {
        val roleType = Preference.catchInt(Preference.ROLETYPE, -1)
        val agentId = Preference.catchInt(Preference.AGENTID, -1)
        if (roleType != 3 && roleType != 7) {
            if (state != -1 && state != 0 && state != agentId) {
                return true
            }
        }
        return false
    }
}
